#include "frmrule.h"


frmRule::frmRule(QWidget *parent) : QDialog(parent), m_ui(new Ui::frmRule)
{
    this->setupForm();
}


frmRule::frmRule(Rule *rl, QWidget *parent) : QDialog(parent), m_ui(new Ui::frmRule)
{
    this->rule = rl;
    this->setupForm();
    this->fillRuleDetails();
    m_ui->txtRuleName->setText(QString((rl->rule_name).c_str()));    
    m_ui->txtToPath->setText(QString(((rl->to_dir).string()).c_str()));
}

void frmRule::setupForm()
{
    m_ui->setupUi(this);

    // connectors
    QObject::connect(m_ui->btnCommands, SIGNAL(accepted()), this, SLOT(btnCommandsSave_clicked()));
    QObject::connect(m_ui->btnCommands, SIGNAL(rejected()), this, SLOT(btnCommandsCancel_clicked()));
    QObject::connect(m_ui->btnBrowseToDir, SIGNAL(clicked()), this, SLOT(btnBrowseToDir_clicked()));
    QObject::connect(m_ui->treeConditions, SIGNAL(itemSelectionChanged()), SLOT(treeConditions_item_changed()));

    QObject::connect(m_ui->btnGetDiscogs, SIGNAL(clicked()), SLOT(btnGetDiscogs_clicked()));

    // cmbAction: setup rule actions
    QStringList actions;
    actions << "Copy" << "Move" << "Delete";
    m_ui->cmbAction->addItems(actions);    

    // set tabl labels
    m_ui->tabWidget->setTabText(0, "Rule Conditions");
    m_ui->tabWidget->setTabText(1, "Discogs Sync");


}


void frmRule::btnGetDiscogs_clicked()
{
    Organiser::http_get_discogs((char*)"label/Def+Recordings+Ltd",frmRule::cb_parse_discogs_response);
}

// callback for http request
size_t frmRule::cb_parse_discogs_response(void *buffer, size_t size, size_t nmemb, void *userp)
{
    cout << "IN DISCOGS RESPONSE CALL BACK" << endl;
    cout << (char*)buffer << endl;
    //vector<Org::Release> releases = Discogs::((char*)buffer);
    return size*nmemb;
}


void frmRule::treeConditions_item_changed()
{
    qDebug("tree clicked");
    QList<QTreeWidgetItem*> items = m_ui->treeConditions->selectedItems();
    QList<QTreeWidgetItem*>::iterator i;
    //QTreeWidgetItem *item = items.first();

}

void frmRule::fillRuleDetails()
{
    TreeWidgetItem_Node *root = new TreeWidgetItem_Node(m_ui->treeConditions);
    root->setText(0, tr("RULE_CONDITIONS"));

    // NEED TO FIX THIS
    populateTreeConditions(rule->root_node->child_nodes, root);
}


void frmRule::populateTreeConditions(const vector<Node> nodes, TreeWidgetItem_Node *treeNode)
{
    vector<Org::Node>::const_iterator i;
    for(i = nodes.begin(); i != nodes.end(); ++i)
    {
        Org::Node cur_node = (*i);
        TreeWidgetItem_Node *treeItem = new TreeWidgetItem_Node(treeNode);

        if(cur_node.node_type == Org::Vars::ORG_NODE_BOOL)
        {
            string o = "";
            (cur_node.operation == Vars::ORG_AND) ? o = "AND" : o = "OR";

            treeItem->setText(0, tr(o.c_str()));
            treeItem->setIcon(0, QIcon(":/icons/tree_j_icon.gif"));
        }
        else if(cur_node.node_type == Org::Vars::ORG_NODE_CONDITION)
        {
            QString search_in = QString((cur_node.search_in).c_str());
            QString search_term = QString((cur_node.search_term).c_str());
            search_in.append("="+search_term);
            treeItem->setText(0, QString(search_in));
            treeItem->setIcon(0, QIcon(":/icons/tree_c_icon.gif"));
        }
        populateTreeConditions(cur_node.child_nodes, treeItem);
    }
}


frmRule::~frmRule()
{
    delete m_ui;
}

void frmRule::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void frmRule::btnBrowseToDir_clicked()\
{

}

void frmRule::btnCommandsSave_clicked()
{
    qDebug("save clicked");

    string rule_name = (m_ui->txtRuleName->text()).toStdString();
    if(rule_name.empty()) return;

    Rule rule(rule_name);
    rule.rule_name = (m_ui->txtRuleName->text()).toStdString();
    rule.action = Vars::ORG_COPY;
    rule.discogs_lookup = (m_ui->txtDiscogsLookup->text()).toStdString();
    rule.to_dir = (m_ui->txtToPath->text()).toStdString();

    // TODO: check if everything is fine
    // save to xml file



}

void frmRule::btnCommandsCancel_clicked()
{
    qDebug("Cancel clicked");
    close();
}
